<?php
namespace app\common\model\quotation;
use think\Model;
use think\Db;
use app\common\model\user\User;

class QuotationList extends Model
{
     // 设置当前模型对应的完整数据表名称
        protected $table = '';
        protected $pk = 'QuotationListId';
        protected $type = '';

        const STATEOK  = 1;
        const STATEINT = 1;
        const STATENO  = 2;
        const STATEDELETE = 404;


    /**
     * 状态分类
     * @param  string $value [description]
     * @return [type]        [description]
     */
    public static function getStatusText($value = '')
    {
        $status = [-1=>'全部订单',1=>'已生效',2=>'已失效'];
        if($value!==''){
            return $status[$value];
        }
        return $status;
    }
    /**
     * 构造函数 复用此模型
     * 用type判断是否存在此表
     * @param [type] $type [description]
     * @param [type] $data [description]
     */
    public function __construct($data=[]){
        if(empty($type)){
            if(empty(input('Type'))){
                moerr();
            }else{
                $type = input('Type');
                if(strstr($type,'hawa')!==false){
                    $type = 'hawa';
                }else if(strstr($type,'buss')!==false){
                    $type = 'buss';
                }
            }
        }
        $tblist = ['hawa','buss'];
        $namelist  = ['company','bussiness'];
        foreach ($tblist as $key => $value) {
            if($type==$value){
                $this->table = 'quotation_'.$namelist[$key].'_list';
                $this->type = $type;
                break;
            }
        }
        if(empty($this->table)){
            moerr('没有找到'.$type.'系统');
        }
        parent::__construct($data);
    }
    
    public function setuserinfo(){
         return $this->hasOne('app\common\model\user\User','UserId','SetUserId');
    }

    /**
     * 获取当前报价
     * @return [type] [description]
     */
    public static function getinfo($id='',$type = 'view'){
        if(empty($id)){
            $today = date('Y-m-d H:i',time());
            $data = QuotationList::where('State',QuotationList::STATEOK)
                             ->where('StartTime','<=',$today)
                                                     ->where('EndTime','>=',$today)
                                                     ->order('QuotationListId desc')
                                                     ->find();
        }else{
            $data = QuotationList::where('QuotationListId',$id)->find();
        }
        if(empty($data)){
            return serr('暂无报价清单');
        }
        if($type =='view'){
            $data = $data->visible(['Price','CarPrice','MaxCarWeight','StartTime','QuotationListId'])->toArray();
        }else if($type=='mana'){
            $data = $data->toArray();
        }
        $data['GoodsList'] = Quotation::getlist($data['QuotationListId']);
        if(!empty($data['SetUserId'])){
            $data['setuserinfo'] = User::getpublicinfo($data['SetUserId']);
        }

        if(strstr(input('Type'),'hawa')!==false){
            $data['HAWAQUOT'] = HAWAQUOT;
        }

        return sok($data);
    }
    /**
     * 根据产品订单查询报价
     * @param  [type] $list [description]
     * @return [type]       [description]
     */
    public static function getquestion($list,$id=''){
        if(empty($id)){
            $today = date('Y-m-d H:i',time());
            $data = QuotationList::where('State',QuotationList::STATEOK)
                               ->where('StartTime','<=',$today)
                               ->where('EndTime','>=',$today)
                               ->order('QuotationListId desc')
                               ->find();
        }else{
            $data = QuotationList::get($id);
        }
        if(empty($data)){
          return serr('暂无报价清单');
        }
        $data = $data->visible(['Price','CarPrice','MaxCarWeight','StartTime','QuotationListId'])->toArray();
        $data['GoodsList'] = Quotation::getquestionlist($data['QuotationListId'],$list);
        return sok($data);
    }

    /**
     * 直接返回报价数据
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function get_create_question($data,$id = ''){
        $rdata = QuotationList::getquestion($data['goodlist'],$id);
        if(!$rdata['code']){
            return serr($rdata['msg']);
        }
        $rdata = $rdata['msg'];
        $sumdata = 0;
        $sumweight = 0;
        $rdata['pricelist'] =[];
        //产废处置费用
        foreach ($data['goodlist'] as $key => $value) {
            $sb = false;
            foreach ($rdata['GoodsList'] as $key2 => $value2) {
                if($value==$value2['GoodId']){
                    $sb = true;
                    $rdata['pricelist'][$key] = $value2['Price'];
                    break;
                }
            }
            if(!$sb){
                $rdata['pricelist'][$key] = $rdata['Price'];
            }
            $sumdata   = $sumdata   + ($rdata['pricelist'][$key] * $data['valuelist'][$key] ) ;
            $sumweight = $sumweight + $data['valuelist'][$key];
        }
        $rdata['Sumdata'] = $sumdata;
        $rdata['Sumweight'] = $sumweight;
        $rdata['CarPrice'] = $rdata['CarPrice']=='0.00'?0:ceil($sumweight/$rdata['MaxCarWeight'])*$rdata['CarPrice'];
        $rdata['QuotationListId'] = $rdata['QuotationListId'];
        $rdata['QuotationCost'] = $rdata['CarPrice'] + $rdata['Sumdata'];
        $rdata['FloatingCost'] = 0;
        $rdata['TotalCost'] =  $rdata['QuotationCost'];
        return sok($rdata);
    }
    /**
     * 新增方法
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function screate($data){
        $userinfo = User::getinfo();
        $data['SetUserId'] = $userinfo->UserId;
        $data['SetTime'] = date('Y-m-d H:i',time());
        $data['State'] = QuotationList::STATEINT;
        $qu = QuotationList::create($data);
        if(empty($qu->QuotationListId)){
            return serr('报价失败');
        }else{
            $data['QuotationListId'] = $qu->QuotationListId;
        }
        if(empty($data['GoodId'])){
            return myok('报价设置成功');
        }
        $qudata = new Quotation($this->type);
        $qudata->screate($data);
        return $data;
    }
    /**
     * 获取订单列表
     * @param  [type] $data    [主数据]
     * @param  string $type    [查询类型/hawa/buss]
     * @param  [type] $is_page [是否分页显示]
     * @return [type]          [description]
     */
    public static function usergetlist($data,$type = 'acco',$is_page = true){

        $rdata = QuotationList::where('State','<>',QuotationList::STATEDELETE);
        $userinfo = User::getinfo();
    
        if($type=='acco'||$type=='mana'){
            $visible = ['QuotationListId','State','StartTime','EndTime','SetTime','setuserinfo.Name','SetUserId'];
        }else{
            return moerr('缺少参数');
        }
        $rdata = $rdata->with(['setuserinfo']);
        $option = come_model_option($data,$rdata,$is_page);
        $rdata = $option['rdata'];
        $data = $option['data'];
        $rdata = $rdata->select();

        $rdata =  $rdata->visible($visible);

        $ndata['list'] = $rdata->toArray();
        $ndata['statetext'] = QuotationList::getStatusText();
        if($is_page){
            $ndata['pageinfo'] = ['pageindex'=>$data['pageindex'],'pagemax'=>$data['count']];
        }
        return $ndata;
    }


  
}
